/**
 * Copyright (2022, ) Institute of Software, Chinese Academy of Sciences
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 *limitations under the License.
 */
package com.gitee.iscas.imports;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wuheng@iscas.ac.cn
 * @since  2.1.1
 * 
 */
public class Analyzer {

	static String dir = "E:\\codes\\iscas-fork\\kubernetes";
	
	static List<String> list = new ArrayList();
	
	static {
//		list.add("github.com/vmware/govmomi");
//		list.add("k8s.io/kubernetes/test/e2e/storage/vsphere");
//		list.add("k8s.io/legacy-cloud-providers/vsphere");
		
		
		list.add("k8s.io/legacy-cloud-providers");
		
//		list.add("github.com/GoogleCloudPlatform/k8s-cloud-provider");
	}
	
	public static void main(String[] args) throws Exception {
		
		File directory = new File(dir);  
		  
        // 调用递归方法开始遍历  
        findGoFiles(directory);  
	}
	
	public static void findGoFiles(File directory) throws IOException {  
        // 确保传入的确实是一个目录  
        if (directory.isDirectory()) {  
            // 获取目录下的所有文件和子目录  
            File[] files = directory.listFiles();  
  
            if (files != null) {  
                for (File file : files) {  
                    // 如果是文件，则检查扩展名  
                    if (file.isFile() && file.getName().endsWith(".go")) {  
                        String contents = readFileToString(file.getAbsoluteFile());
                        
                        for (String str : list) {
                        	if (contents.contains(str)) {
                        		System.out.println(file.getAbsolutePath());
                        	}
                        }
                    }  
                    // 如果是目录，则递归调用  
                    else if (file.isDirectory()) {  
                        findGoFiles(file);  
                    }  
                }  
            }  
        } 
    }  

	// 辅助方法：将文件内容读取为String  
    public static String readFileToString(File file) throws IOException {  
        StringBuilder stringBuilder = new StringBuilder();  
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {  
            String line;  
            while ((line = reader.readLine()) != null) {  
                stringBuilder.append(line).append("\n");  
            }  
        }  
        return stringBuilder.toString();  
    }  

}
